<template>
    <div>
      <Home_head />
      <div class="personalCenter">
        <div class="user_info">
          <el-upload
          class="avatar-uploader"
          action=""
          :http-request="uploadImge"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :headers="MyHeader"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar1" />
          </el-upload>
          <span class="user_name">{{ userName }}</span>
          <span type="text" class="user_note"  id="user_note">{{userNote}}</span>
        </div>
        <div class="navigationBar">
          <span class="bar" @click="topersonalCenter()">文章</span>
          <span class="bar" @click="toCollectView()">收藏</span>
          <span class="bar" @click="toAttentionView()">关注</span>
          <span class="bar" @click="toSetView()">设置</span>
          <span style="position: absolute; right: 30px;">关注：{{ attentionNum }}</span>
          <span style="position: absolute; right: 120px;">粉丝: {{ fansNum }}</span>
        </div>
        <h1>我的关注</h1>
        <hr>
        <div v-for="count in UserAttentionInfo.length" :key="count">
            <div class="UserAttentionInfo">
            <img :src="UserAttentionInfo[count-1].userHead" style="width: 40px; height: 40px; border-radius: 50%;">
            <span class="UserAttentionInfo_userName">{{UserAttentionInfo[count-1].userName}}</span>
            <span class="UserAttentionInfo_userNote">{{UserAttentionInfo[count-1].userNote}}</span>
            <button class="UserAttentionInfo_attention">已关注</button>
        </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  import axios from "axios";
  import Home_head from "@/components/Home_head.vue";
  var token = localStorage.getItem("user_Token");
  export default {
    components: {
      Home_head,
    },
    data(){
      return{
        imageUrl:"",
        MyHeader: { Authorization: token },
        userName: "罗添煦",
        userNote: "这个人什么都没有留下",
        attentionNum:Number,
        fansNum:Number,
        UserAttentionInfo:[],
    }
    },
    created(){
        this.queryInfo()
        this.getUserAttentionInfo()
    },
    methods:{
      uploadImge(param){
      const formData = new FormData()
      formData.append('file', param.file)
      formData.append("token",token)
      const url = '/uploadImge'
      axios.post(url,formData).then(data => {
        console.log('上传图片成功')
        this.$message({
                message: '头像切换成功',
                type: 'success'
                });
        location.reload();
      }).catch(response => {
       console.log('图片上传失败')
     })
    },
      handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
      queryInfo(){
        axios.post("/queryInfo", {
                  userToken : localStorage.getItem("user_Token"),
              }).then(response=>{
                this.userName=response.data.userName
                this.imageUrl=response.data.userHead
                if(response.data.userNote!=""){
                  if(response.data.userNote.length<10){
                   this.userNote=response.data.userNote
                   this.userAllNote=response.data.userNote
                  }else{
                    this.userNote=response.data.userNote.substring(0,9)+"......"
                    this.userAllNote=response.data.userNote
                  }
                }else{
                  this.userNote="这个人什么都没有留下"
                }
                this.fansNum=response.data.fansNum
                this.attentionNum=response.data.attentionNum
              })
      },
      getUserAttentionInfo(){
        axios.post("/getUserAttentionInfo", {userToken : localStorage.getItem("user_Token"),}).then((response)=>{
            this.UserAttentionInfo=response.data
        })
      },
      topersonalCenter(){
          this.$router.push("/personalCenter")
      },
      toCollectView(){
        this.$router.push("/collect")
      },
      toAttentionView(){
        this.$message({
                message: '请不用重复点击同一个url!',
                type: 'error'
                });
      },
      toSetView(){
        this.$router.push("/Set")
      },
    },
  }
  </script>
  
  <style>
  .personalCenter {
    position: absolute;
    top: 50px;
    left: 350px;
    background-color: #fff;
    width: 900px;
    height: 800px;
  }
  .navigationBar{
    width: 100%;
    height: 45px;
    display: flex;
    align-items: center;
    background-color: #ffffff;
    border-bottom: #000000 solid 1.5px;
  }
  .user_info {
    display: block;
    background-image: url(../assets/580cbb24aff1e6005043fb5b9c216.jpg);
    height: 300px;
  }
  .user_name {
    position: absolute;
    left: 130px;
    top: 155px;
    font-size: 23px;
    color: #000000;
  }
  .user_note {
    position: absolute;
    left: 130px;
    top: 190px;
    border: 0px;
    background-color: rgba(0,0,0,0);
  }
  .bar{
      color: #000000;
      margin-left: 30px;
  }
  
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 50%;
    cursor: pointer;
    position: absolute;
    left: 20px;
    top: 150px;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
  }
  .avatar1 {
    width: 80px;
    height: 80px;
    display: block;
  }
  .UserAttentionInfo{
    display: block;
    position: relative;
    margin-left: 50px;
    margin-top: 20px;
    border-bottom: 1px solid;
  }
  .UserAttentionInfo .UserAttentionInfo_userName{
    position: absolute;
    left: 45px;
  }
  .UserAttentionInfo .UserAttentionInfo_userNote{
    position: absolute;
    left: 45px;
    top: 24px;
}
    .UserAttentionInfo .UserAttentionInfo_attention{
        position: absolute;
        right: 30px;
        top: 10px;
    }
  </style>